<?php
declare (strict_types = 1);

namespace app\command;

use app\data\service\SchemaService;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class TableSchema extends Command
{
  protected function configure()
  {
    // 指令配置
    $this->setName('tableSchema')
      ->addArgument('table_name',Argument::REQUIRED,'表名')
      ->addOption('type',null,Option::VALUE_REQUIRED,'打印类型，1、打印表类型，2、打印表注释')
      ->setDescription('生成表结构');
  }

  protected function execute(Input $input, Output $output)
  {
    // 指令输出
    $tableName = $input->getArgument('table_name');
    $schemaService = new SchemaService();
    $list = $schemaService->getSchemaInfo($tableName);
    if(empty($list)){
      $output->writeln('未找到该表信息，请检查表名是否正确!!!');
    }

    $json = '';

    if($input->getOption('type') == 1 || empty($input->getOption('type'))){
      foreach ($list as $key => $value){
        if($key == count($list)-1){
          $temp = '"'.$value['COLUMN_NAME'].'"'.": ".'"'.$value['DATA_TYPE'].'"'.PHP_EOL;
        }else{
          $temp = '"'.$value['COLUMN_NAME'].'"'.": ".'"'.$value['DATA_TYPE'].'",'.PHP_EOL;
        }
        $json .= $temp;
      }
    }

    if($input->getOption('type') == 2){
      foreach ($list as $item => $v){
        if($item == count($list)-1){
          $temp = '"'.$v['COLUMN_NAME'].'"'.": ".'"'.$v['COLUMN_COMMENT'].'"'.PHP_EOL;
        }else{
          $temp = '"'.$v['COLUMN_NAME'].'"'.": ".'"'.$v['COLUMN_COMMENT'].'",'.PHP_EOL;
        }
        $json .= $temp;
      }
    }


    $schemaJson = '{'.PHP_EOL.
      $json.
      '}';

    $result = json_decode($schemaJson,true);
    var_export($result);
    exit(0);
  }
}
